<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简易的图书管理系统</title>
    <link rel="stylesheet" href="./bootstrap.min.css">
    <script src="./jQuery.min.js"></script>
    <style>
        /* div {
            margin-left: ;
        } */
    </style>
</head>

<body style="padding: 20px;">
    <!-- 添加图书的panel面板 -->
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">添加新图书</h3>
        </div>
        <div class="panel-body form-inline text-center">
            <div class="input-group" style="margin: 2.6667vw;">
                <div class="input-group-addon">书---名</div>
                <input type="text" class="form-control" id="iptBookname" placeholder="请输入书名">
            </div>
            <div class="input-group" style="margin: 2.6667vw;">
                <div class="input-group-addon">作---者</div>
                <input type="text" class="form-control" id="iptAuthor" placeholder="请输入作者">
            </div>
            <div class="input-group" style="margin: 2.6667vw;">
                <div class="input-group-addon">出版社</div>
                <input type="text" class="form-control" id="iptPublisher" placeholder="请输入出版社">
            </div>
            <button id="btnAdd" class="btn btn-primary" style="margin: 2.6667vw;">添---加 </button>
        </div>
    </div>

    <!-- 图书的表格 -->

    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>Id</th>
                <th>书名</th>
                <th>作者</th>
                <th>出版社</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody id="tb">

        </tbody>
    </table>

    <script>
        $(function () {
            //获取图书数据
            function getBookList() {
                $.ajax({
                    type: "GET",  //默认get
                    url: "http://www.liulongbin.top:3006/api/getbooks",  //默认当前页
                    data: "data",  //格式{key:value}
                    dataType: "json",
                    beforeSend: function () { }, //请求发送前回调,常用验证
                    success: function (response) {  //请求成功回调
                        if (response.status !== 200) {
                            return alert('获取失败');
                        }
                        var rows = [];
                        $.each(response.data, function (i, item) {
                            rows.push('<tr><td>' + item.id + '</td><td>' + item.bookname + '</td><td>' + item.author + '</td><td>' + item.publisher + '</td><td><a href="javascript:;" class="del" data-id="' + item.id + '">删除</a></td></tr>')
                        })
                        $('#tb').empty().append(rows.join(''));
                    }
                })
            }
            getBookList();
            //因为绑定点击事件的时候 a还没有渲染出来 所以给tbody创建点击事件，最后代理给a
            // $('.del').on('click', function () {
            // })
            $('tbody').on('click', '.del', function () {
                // attr() 方法设置或返回被选元素的属性值
                var id = $(this).attr('data-id');
                //get可以及时刷新，而ajax不能
                $.get('http://www.liulongbin.top:3006/api/delbook', { id: id }, function (res) {
                    if (res.status !== 200)
                        return alert('删除失败');
                    getBookList();
                })
            })

            //给添加按钮绑定
            $('#btnAdd').click(function () {
                var bookname = $('#iptBookname').val().trim();
                var author = $('#iptAuthor').val().trim();
                var publisher = $('#iptPublisher').val().trim();
                if (bookname.length <= 0 || author.length <= 0 || publisher <= 0) {
                    return alert('请填写完整的图书信息');
                }
                $.post("http://www.liulongbin.top:3006/api/addbook",
                    { bookname: bookname, author: author, publisher: publisher },
                    function (res) {
                        if (res.status !== 201) {
                            return alert('添加图书失败')
                        }
                        getBookList();
                        var bookname = $('#iptBookname').val('');
                        var author = $('#iptAuthor').val('');
                        var publisher = $('#iptPublisher').val('');
                    }
                )
            })
        })
    </script>
</body>

</html>